package com.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.member.command.CollegeCmd;
import com.member.controller.validation.collegeAddUpdate;
import com.member.exception.CustomException;
import com.member.po.College;
import com.member.po.Page;
import com.member.po.Student;
import com.member.service.CollegeService;

@Controller
@RequestMapping("/college")
public class CollegeController {
	//注入DepartmentService
	@Autowired
	private CollegeService collegeService;
	
	
	//部门信息列表页面
	@RequestMapping("/college")
	public  String college(CollegeCmd collegeCmd ,Model model,HttpServletRequest request){
		Map map=new HashMap();
		Page page = new Page();
		map.put("size", page.getSize());
		map.put("pageNum", page.getPageNum());
		if(collegeCmd.getPageNum() != null){
			page.setPageNum(Integer.parseInt(collegeCmd.getPageNum()));
			map.put("pageNum", page.getPageNum());
		}
		if(collegeCmd.getCollegeId() != null){
			map.put("collegeId", Integer.parseInt(collegeCmd.getCollegeId()));
		}
		if(collegeCmd.getCollegeName() != null){
			map.put("collegeName", Integer.parseInt(collegeCmd.getCollegeName()));
		}
		List<College> collegeList=collegeService.select(map);
		page.setAmontRow(collegeService.countRows(map));
		model.addAttribute("page", page);
		model.addAttribute("collegeList", collegeList);
		return "/baseInfo/college/collegeList";		
	}
	
	@ModelAttribute("colleges")
	public List<College> getCollegeTypes() {		
		List<College> collegeList;
		try {
			collegeList = collegeService.select(null);
			return collegeList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/editCollege", method = { RequestMethod.POST,RequestMethod.GET })	
	public String editCollege(Model model,@RequestParam(value = "id", required = true) Integer items_id)
			throws Exception {
		College college=collegeService.selectById(items_id);			
		if(college == null){
			throw new CustomException("修改的部门信息不存在!");
		}	
		model.addAttribute("items", college);
		return "/baseInfo/college/collegeUpdate";
	}
	
	@RequestMapping("/updateCollegeSubmit")
	public String updateCollegeSubmit(Model model,	HttpServletRequest request,	Integer id,
		@Validated(value={collegeAddUpdate.class}) College college ,BindingResult result )
	{
		if(result.hasErrors())
		{
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
			model.addAttribute("items", college);
			return "/baseInfo/college/collegeUpdate";
		}
		try {
			collegeService.updateById(college);
			return "redirect:/college/college";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	
	}
	
	@RequestMapping("/addCollege")	
	public String addCollege()
	{	     
		return "/baseInfo/college/collegeAdd";
	}
	
	@RequestMapping(value = "/addCollegeSubmit")	
	public String addClazzSubmit(Model model,
			@Validated (value={collegeAddUpdate.class}) College college,BindingResult result)
	{	
		if(result.hasErrors())
		{
			
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
		    model.addAttribute("items", college);
		    return "/baseInfo/college/collegeAdd";
		}
		
		try {
			collegeService.insert(college);
			return "redirect:/college/college";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	//按ID删除部门信息
	@RequestMapping(value = "/deleteCollege")
	public String deleteCollege(Model model,@RequestParam(value = "id", required = true) Integer items_id)
	{
		try {
			collegeService.deleteById(items_id);
			return "redirect:/college/college";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping("/deleteColleges")
	public String deleteColleges(Model model, Integer[] id){
		if(id != null){
			for(Integer i:id){
				collegeService.deleteById(i);
			}
			return "redirect:/college/college";
		}
		return "error";
	}
	
	

}
